Public Class frmImportForm
    'form for creating new aMod Solution

#Region "Attributes"
#Region "Private attributes"
    Private MoDfrmImportForm As ClsDfrmImportForm = Nothing 'control object for this form
    Private MoAction As ClsAction = New ClsAction(GeAction.kkNoAction) 'action for this form
    Private MeResult As GeActionResult = GeActionResult.kkOk  'result of this form
    Private MbReadyForUse As Boolean = False 'indicates proper creation or first initialization of form

    Private MoMarkedUnits As Dictionary(Of String, String) = Nothing 'units with formular paths for import
#End Region
#End Region
#Region "Constructors & Init"
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        '**********************************************************************
        InitControl()
        '**********************************************************************
    End Sub
    'setting form settings before its using
    Private Function InitControl() As GeActionResult
        SetReadyForUse(False) 'disabling form
        Try
            StartPosition = FormStartPosition.CenterParent
            WindowState = FormWindowState.Normal
            Size = New Size(700, 400)
            MinimumSize = New Size(800, 500)
            ShowInTaskbar = False
            ShowIcon = True
            Icon = ConvertImageToIcon(imlFormImages.Images("FORMICON"))
            Text = "Importovanie unitov z formularov"

            AcceptButton = btnOk
            CancelButton = btnCancel

            UcnGrid.tlTreeList.OptionsSelection.EnableAppearanceFocusedCell = False
            ucnGrid.tlTreeList.OptionsSelection.EnableAppearanceFocusedRow = True

            Dim LoRepositoryItemButtonEdit As DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit = New DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
            LoRepositoryItemButtonEdit.Name = "MyRepositoryItemButtonEdit"
            AddHandler LoRepositoryItemButtonEdit.ButtonClick, AddressOf RepositoryItemButtonEditButtonClicked
            ucnGrid.tlTreeList.RepositoryItems.Add(LoRepositoryItemButtonEdit)

            SetReadyForUse(True) 'enabling form
            Return GeActionResult.kkOk
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#Region "Properties"
#Region "Public properties"
#End Region
#Region "Private properties"
    Private ReadOnly Property FocusedRow() As DevExpress.XtraTreeList.Nodes.TreeListNode
        Get
            Return ucnGrid.tlTreeList.FocusedNode()
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
#Region "frmVersionsManagement - Events handler"
    Private Sub frmVersionsManagement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'user closed via the forms control box close button (X) or via Me.Close - I set Me.DialogResult to Windows.Forms.DialogResult.OK when leaving form in successful situation
        If e.CloseReason = System.Windows.Forms.CloseReason.UserClosing AndAlso Me.DialogResult = Windows.Forms.DialogResult.Cancel Then
            MeResult = GeActionResult.kkCancel
        End If
        'since sometimes form is going to close and I dont know why, here is place for fixing the problem :-)
        Select Case e.CloseReason
            Case CloseReason.None
                e.Cancel = True
        End Select
    End Sub
#End Region
#Region "Buttons - Events handler"
    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        If MoMarkedUnits.Count > 0 Then
            MoAction.SetData(MoMarkedUnits, "UNITSFORIMPORT")
            MeResult = GeActionResult.kkOk
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        MeResult = GeActionResult.kkCancel
        Me.Close()
    End Sub
#End Region
#Region "ucnGrid - Events handler"
    Private Sub ucnGrid_MMColumnProperties(ByRef Column As DevExpress.XtraTreeList.Columns.TreeListColumn) Handles UcnGrid.MMColumnProperties
        Try
            Select Case Column.FieldName
                Case "UnitName"
                    Column.Caption = "Progr. nazov unitu"
                    Column.VisibleIndex = 1
                    Column.OptionsColumn.AllowEdit = False
                Case "HumanUnitName"
                    Column.Caption = "Anal. nazov unitu"
                    Column.VisibleIndex = 0
                    Column.OptionsColumn.AllowEdit = False
                    Column.SortOrder = SortOrder.Ascending
                Case "FormPathForImport"
                    Column.Caption = "Importovany formular"
                    Column.VisibleIndex = 3
                    Column.OptionsColumn.AllowEdit = True
                    Column.ColumnEdit = ucnGrid.tlTreeList.RepositoryItems("MyRepositoryItemButtonEdit")
                Case Else
                    'Column.Caption will be the same like Column.FieldName
                    Column.VisibleIndex = -1
                    Column.OptionsColumn.AllowEdit = False
            End Select
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGrid_MMCustomDrawNodeCell(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles UcnGrid.MMCustomDrawNodeCell
        If e.Node("FormPathForImport") <> "" Then
            e.Appearance.BackColor = Color.Green
        End If
    End Sub
    Private Sub ucnGrid_MMFillDataSource(ByVal Item As Object, ByRef DataSource As Microsoft.VisualBasic.Collection) Handles UcnGrid.MMFillDataSource
        Try
            For Each LoDataRow As DataRow In MoDfrmImportForm.AModOneVersion.DSaModVersion.UnitsTable.Rows
                DataSource.Add(New ClsUcnGridUnboundItemImportUnitFromForm(True, False, LoDataRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.UnitName), LoDataRow(AMod2.DsAModOneVersion.TableSysnames.Units.Columns.HumanUnitName), "", 0))
            Next
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub
    Private Sub ucnGrid_MMRowDoubleClicked(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode) Handles ucnGrid.MMRowDoubleClicked
        btnOk_Click(Me, Nothing)
    End Sub
    Private Sub ucnGrid_MMValueChanged(ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByVal NewValue As Object, ByVal OldValue As Object) Handles ucnGrid.MMValueChanged
        SetEnable()
    End Sub
    Private Sub ucnGrid_MMValueValidating(ByVal Item As Object, ByVal Row As DevExpress.XtraTreeList.Nodes.TreeListNode, ByVal Column As DevExpress.XtraTreeList.Columns.TreeListColumn, ByVal Value As Object, ByRef Valid As Boolean, ByRef ErrorText As String) Handles UcnGrid.MMValueValidating
        If Column.FieldName = "FormPathForImport" Then
            Valid = True
        End If
    End Sub
#End Region
#End Region
#Region "Functions"
    'gets all marked versions
    Private Function GetMarkedUnits() As Dictionary(Of String, String)
        Dim LoMarkedUnits As Dictionary(Of String, String) = New Dictionary(Of String, String)

        For Each LoNode As DevExpress.XtraTreeList.Nodes.TreeListNode In ucnGrid.tlTreeList.Nodes
            If LoNode("FormPathForImport") <> "" Then LoMarkedUnits.Add(LoNode("UnitName"), LoNode("FormPathForImport"))
        Next

        Return LoMarkedUnits
    End Function
    'opens open dialog for choosing a path to form
    Private Function ChoosePath() As String
        Dim LsResultPath As String = ""

        OpenFileDialog.InitialDirectory = IO.Path.GetDirectoryName(MoDfrmImportForm.AmodPath)
        OpenFileDialog.Filter = "Subory .Designer.vb(*.Designer.vb)|*.Designer.vb"
        OpenFileDialog.Title = "Otvorenie formulara (Designer cast) pre import"
        If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            LsResultPath = OpenFileDialog.FileName
        End If

        Return LsResultPath
    End Function
#End Region
#Region "Subs"
    'set enable/disable for controls
    Private Sub SetEnable()
        MoMarkedUnits = GetMarkedUnits()

        btnOk.Enabled = MoMarkedUnits.Count > 0
    End Sub
    'sets MbReadyForUse attribute and sets Enable property of form
    Private Sub SetReadyForUse(ByVal IbValue As Boolean)
        MbReadyForUse = IbValue
        Me.Enabled = IbValue 'enabling form
    End Sub
    'sub for processing click on the 3 dots button on repository item DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit
    Private Sub RepositoryItemButtonEditButtonClicked(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
        Dim LsPathToForm As String = ChoosePath()
        If LsPathToForm <> "" Then
            ucnGrid.tlTreeList.FocusedNode.SetValue(ucnGrid.tlTreeList.FocusedColumn, LsPathToForm)
            'ucnGrid.tlTreeList.ActiveEditor.EditValue = LsPathToForm - I cant use this, coz I lost focus to open dialog so theres no ActiveEditor!!!
            ucnGrid.tlTreeList.PostEditor()
            'since there no event for this situation I call manually refreshing of controls
            SetEnable()
        Else
            'do nothing, previous value of cell remains
        End If
    End Sub
#End Region

    '#######################################
    'execute function
    Public Function Execute(ByRef OoAction As ClsAction) As GeActionResult
        Try
            MoAction = OoAction
            '---------------------------------------------
            'ACTION BEFORE ACTION
            '---------------------------------------------

            '---------------------------------------------
            'ACTION
            '---------------------------------------------
            Select Case MoAction.Action
                Case GeAction.kkamodImportUnitFromForm
                    MoDfrmImportForm = MoAction.GetData("CLSDFRMIMPORTFORM")
                    ucnGrid.Init(Nothing)
                    SetEnable()

                    Me.ShowDialog()
            End Select
            '---------------------------------------------
            'ACTION AFTER ACTION
            '---------------------------------------------

            '---------------------------------------------
            Return MeResult
            '---------------------------------------------

        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    '#######################################

End Class